package com.awenhui.demo.controller;

import com.awenhui.demo.module.samsql.utils.MyBatisSqlUtils;
import com.awenhui.demo.module.samsql.dao.SamSqlMapper;
import com.awenhui.demo.module.samsql.dao.SamSqlParamInMapper;
import com.awenhui.demo.module.samsql.model.entity.SamSqlEntity;
import com.awenhui.demo.module.samsql.model.entity.SamSqlParamInEntity;
import com.awenhui.demo.dao.UserMapper;
import com.awenhui.demo.model.rbac.UserModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/mybatis")
public class MyBatisController {


    @Autowired
    private UserMapper userMapper;

    @GetMapping("/getUser")
    public Object getUser() {
        List<UserModel> pagingUser = userMapper.getPagingUser();
        return pagingUser;

    }

    @GetMapping("/getUserByName")
    public Object getUserByName(String name, String mobile) {
        List<UserModel> pagingUser = userMapper.getPagingUserByName(name, mobile, 10);
        return pagingUser;
    }

    @GetMapping("/getUserByMap")
    public Object getUserByMap() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "yuxf");

        List<Map<String, Object>> pagingUserByMap = userMapper.getPagingUserByMap(map);
        return pagingUserByMap;
    }





    //https://blog.csdn.net/u014746226/article/details/103202672
    @GetMapping("/testBeanMap")
    public Object testBeanMap() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "于新方");
        map.put("age", 18);
        map.put("sex", Sex.Man);
        Map<?, Object> beanMap = new BeanMap(map);


        return beanMap;
    }

    enum Sex {
        Man, Female
    }

    @Autowired
    private SamSqlMapper samSqlMapper;

    @Autowired
    private SamSqlParamInMapper sampSqlParamInMapper;


    @GetMapping("/testSamSql")
    public Object testSamSql(HttpServletRequest request) {
        SamSqlEntity samSql = samSqlMapper.getSqlByCode("test");
        List<SamSqlParamInEntity> samSqlParamInList = sampSqlParamInMapper.findSamSqlParamInList(samSql.getId());
        String sql = MyBatisSqlUtils.getSql(request, samSql.getSqlStr(), samSqlParamInList);
        Map<String, Object> sqlResult = samSqlMapper.findSqlResult(sql);

        return sqlResult;
    }


    @GetMapping("/testParse")
    public Object testParse(HttpServletRequest request) {
        String text = "   <select>\n" +
                "         select * from ua_user\n" +
                "         <where>\n" +
                "             <if test=\"name!=null and name != ''\">\n" +
                "                 username like '%${name}%'\n" +
                "             </if>\n" +
                "             and mobile like #{mobile}\n" +
           //     "             <if test=\"stateList!=null and stateList.size() > 0\">\n" +
                "<foreach collection=\"stateList\" item=\"item\" open=\"AND state in (\" separator=\",\" close=\")\">" +
                "#{item}" +
                "</foreach>" +
               // "             </if>\n" +
                "<foreach collection=\"flowList\" item=\"item\" open=\"AND flow in (\" separator=\",\" close=\")\">" +
                "#{item}" +
                "</foreach>" +
                "         </where>\n" +
                "    </select>";
        Map<String, Object> map = new HashMap<>();
        map.put("name", "何刘惠");
        map.put("mobile", "1885'6'3;21452");
        map.put("stateList", Arrays.asList(1, 2, 3, 4));
        map.put("flowList", Arrays.asList("start", "step1", "setp2", "step3"));


        List<SamSqlParamInEntity> sqlParamList = new ArrayList<>();
        SamSqlParamInEntity nameParam = new SamSqlParamInEntity();
        nameParam.setName("name");
        nameParam.setDataType("String");
        nameParam.setDefaultValue("何刘惠");
        nameParam.setIsArray(false);
        nameParam.setIsNull(1);

        SamSqlParamInEntity mobileParam = new SamSqlParamInEntity();
        mobileParam.setName("mobile");
        mobileParam.setDataType("String");
        mobileParam.setDefaultValue("18786703675");
        mobileParam.setIsArray(false);
        mobileParam.setIsNull(1);

        SamSqlParamInEntity stateListParam = new SamSqlParamInEntity();
        stateListParam.setName("stateList");
        stateListParam.setDataType("Integer");
        stateListParam.setIsArray(true);
        stateListParam.setDefaultValue("1,8,2,6,4");
        stateListParam.setIsNull(1);

        SamSqlParamInEntity flowListParam = new SamSqlParamInEntity();
        flowListParam.setName("flowList");
        flowListParam.setDataType("String");
        flowListParam.setIsArray(true);
        flowListParam.setDefaultValue("step1,step2,step3");
        flowListParam.setIsNull(1);

        sqlParamList.add(nameParam);
        sqlParamList.add(mobileParam);
     //   sqlParamList.add(stateListParam);
        sqlParamList.add(flowListParam);


        String sql = MyBatisSqlUtils.getSql(request, text, sqlParamList);
        //  String sql = test0204(text,map);
        log.info(sql);
        return sql;

    }





}
